﻿CREATE PROCEDURE [dbo].[yaf_topic_move](
                @TopicID   INT,
                @ForumID   INT,
                @ShowMoved BIT)
AS
    BEGIN
        DECLARE  @OldForumID INT
                             
        SELECT @OldForumID = ForumID
        FROM   yaf_Topic
        WHERE  TopicID = @TopicID
                  
        IF @ShowMoved <> 0
        BEGIN
            -- create a moved message
            INSERT INTO yaf_Topic
                       (ForumID,
                        UserID,
                        UserName,
                        Posted,
                        Topic,
                        Views,
                        Flags,
                        Priority,
                        PollID,
                        TopicMovedID,
                        LastPosted,
                        NumPosts)
            SELECT ForumID,
                   UserID,
                   UserName,
                   Posted,
                   Topic,
                   0,
                   Flags,
                   Priority,
                   PollID,
                   @TopicID,
                   LastPosted,
                   0
            FROM   yaf_Topic
            WHERE  TopicID = @TopicID
        END
        
        -- move the topic
        UPDATE yaf_Topic
        SET    ForumID = @ForumID
        WHERE  TopicID = @TopicID
                  
        -- update last posts
        EXEC yaf_forum_updatelastpost
             @OldForumID
        EXEC yaf_forum_updatelastpost
             @ForumID
    END


